package com.example.studentmanager.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.studentmanager.R;
import com.example.studentmanager.db.StudentManager;

public class RegisterActivity extends AppCompatActivity {

    private EditText etUsername;
    private EditText etPassword;
    private EditText etName;
    private EditText etClass;
    private EditText etNo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        etName = findViewById(R.id.etName);
        etClass = findViewById(R.id.etClass);
        etNo = findViewById(R.id.etNo);

        findViewById(R.id.btRegister).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
    }

    private void register() {
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        String name = etName.getText().toString();
        String clazz = etClass.getText().toString();
        String numberId = etNo.getText().toString();
        if (StudentManager.isExistsStudent(username)) {
            Toast.makeText(this, "学生已经存在", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StudentManager.registerStudent(username, password, name, clazz, numberId)) {
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
        }
        finish();
    }
}
